package com.lxl.controller.department;

import com.lxl.common.dto.ResultEntity;
import com.lxl.pojo.Department;
import com.lxl.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Date 2021/11/30 16:28
 * @Created by LXLDEMO
 */
@RestController
@Api(tags = "department4swagger2")
@RequestMapping("/department4swagger2")
public class DepartmentController4Swagger2 {
    @Autowired
    private DepartmentService departmentService;


    /**
     * 测试接口
     * ApiOperation 接口描述
     * GetMappin 请求方式：get  请求路径：""
     * GetMappin:：
     *      不写值，默认是"",即使用http:localhost:8082/test即可访问，
     *      注意同一个controller中不能有两个相同的路径
     * @return
     */
    @ApiOperation("测试接口")
    @GetMapping
    public String success(){
        return "test succeed.";
    }

    @ApiOperation("获取所有department信息")
    @GetMapping("/getAll")
    public ResultEntity getAll(){
        List<Department> all = departmentService.getAll();
        return ResultEntity.ofSuccess(all);
    }

    /**
     * 根据id获取 department信息
     * @param id
     * @return
     */
    @ApiOperation("根据id获取department信息")
    @PostMapping("/getById")
    @ResponseBody
    public ResultEntity getDepartmentById(@RequestBody Integer id){
        Map<String,Object> param = new HashMap<>();
        param.put("id","" + id);
        Department byId = departmentService.getById(param);
        return ResultEntity.ofSuccess(byId);
    }

    /**
     * 根据id删除 department信息
     * @param id
     * @return
     */
    @ApiOperation("根据id删除department信息")
    @DeleteMapping("/delById")
    @ResponseBody
    public ResultEntity delById(@RequestBody Integer id){
        Map<String,Object> param = new HashMap<>();
        param.put("id","" + id);
        String result = departmentService.delById(param);
        return ResultEntity.ofSuccess(result);
    }

    /**
     * 增加 department信息
     * @param department
     * @return
     */
    @ApiOperation("新增 not map")
    @PostMapping("/addDepartment")
    @ResponseBody
    public ResultEntity addDepartment(@RequestBody Department department){
        Map<String,Object> param = new HashMap<>();
        param.put("id",department.getId());
        param.put("departmentName",department.getDepartmentName());
        System.out.println(department);
        String result = departmentService.addItem(param);
        return ResultEntity.ofSuccess(result);
    }


    /**
     * 更新 department信息
     * @param department
     * @return
     */
    @ApiOperation("新增")
    @PostMapping("/updateDepartmentById")
    @ResponseBody
    public ResultEntity updateDepartmentById(@RequestBody Department department){
        System.out.println(department);
        Map<String,Object> param = new HashMap<>();
        param.put("id",department.getId());
        param.put("departmentName",department.getDepartmentName());
        System.out.println(department);
        String result = departmentService.updateItem(param);
        return ResultEntity.ofSuccess(result);
    }
}
